package com.example.gaming;

import java.util.ArrayList;

import com.example.ajouconqueror.R;
import com.example.classes.ABuildingInR;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;
import android.media.SoundPool.OnLoadCompleteListener;
import android.os.Bundle;
import android.os.Vibrator;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

public class conqueringActingMission extends Activity implements SensorEventListener {
	
	private float oriN = 0;
	private float oriE = 90;
	private float oriS = 180;
	private float oriW = 270;
	private int setR1 = 0;
	private int setR2 = 0;
	private int setR3 = 0;
	private int setR4 = 0;
	private int setL1 = 0;
	private int setL2 = 0;
	private int setL3 = 0;
	private int setL4 = 0;
	private int count = 0;
	MediaPlayer backgroundPlayer;
	SoundPool pool;	
	private Vibrator vibrator;
	private TextView textori;
	private TextView textturn;
	SensorManager sensorManager = null;
	Intent intent = null;
	int track1, track2;
	boolean[] loaded = new boolean[3];
	private static final int REQUEST_QUESTION_MISSION = 101;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		intent = getIntent();
		sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
		setContentView(R.layout.activity_conquering_acting_mission);
		// just some textviews, for data output
		textori = (TextView) findViewById(R.id.textori);
		textturn = (TextView) findViewById(R.id.textturn);
		
		backgroundPlayer = MediaPlayer.create(this, R.raw.test_cbr);
		backgroundPlayer.setLooping(true);
		
		pool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
		pool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
            @Override
            public void onLoadComplete(SoundPool soundPool, int sampleId,
                    int status) {
            	Log.d("sound", "Loaded: "+sampleId);
                if(track1 == sampleId) loaded[0] = true;
                else if(track2 == sampleId) loaded[1] = true;
            }
        });
		
		track1 = pool.load(this, R.raw.track1, 0);
		track2 = pool.load(this, R.raw.back, 0);
		 
		String vibratorService = Context.VIBRATOR_SERVICE;
		vibrator = (Vibrator)getSystemService(vibratorService);
	}
	
	@Override
	protected void onPause() {
		pool.autoPause(); 
		backgroundPlayer.pause();
		super.onPause();
	}
	@Override
	protected void onResume() {
		super.onResume();
		pool.autoResume(); 
		backgroundPlayer.start();
		sensorManager.registerListener(this,
				sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
				sensorManager.SENSOR_DELAY_GAME);
	}

	protected void onDestroy() {
		// release sound pool resources
		pool.release();	
		if(backgroundPlayer.isPlaying())
			backgroundPlayer.stop();
		backgroundPlayer.release();		
		super.onDestroy();
	}

	@Override
	protected void onStop() {
		super.onStop();
		sensorManager.unregisterListener(this,
				sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION));
	}
	
	
	@Override
	public void onBackPressed() {
		doVibration2() ;
		
		if(loaded[1]) {
			pool.play(track2, 1, 1, 0, 0, 1);			
		} 
	}

	public void onSensorChanged(SensorEvent event) {
		synchronized (this) {
			if (event.sensor.getType() == Sensor.TYPE_ORIENTATION) {
				if (event.values[0] > oriN && event.values[0] < oriE) {
					textori.setText("North");
					setR1 = 1;
					setL1 = 1;
				} else if (event.values[0] > oriE && event.values[0] < oriS
						&& (setR1 == 1 || setL1 == 1)) {
					textori.setText("East");
					setR2 = 1;
					setL2 = 1;
				} else if (event.values[0] > oriS && event.values[0] < oriW
						&& (setR2 == 1 || setL2 == 1)) {
					textori.setText("South");
					setR3 = 1;
					setL3 = 1;
				} else if (event.values[0] > oriW && (setR3 == 1 || setL3 == 1)) {
					textori.setText("West");
					setR4 = 1;
					setL4 = 1;
					count += 1;
					
					if(loaded[0]) {
						pool.play(track1,  1,  1, 0, 0, 0.5f);			
					} 
					doVibration();
					textturn.setText(Integer.toString(count));
					setR1 = 0;
					setR2 = 0;
					setR3 = 0;
					setR4 = 0;
					setL1 = 0;
					setL2 = 0;
					setL3 = 0;
					setL4 = 0;
				}
				if (count == 2) {
					count=0;
					Intent qIntent = new Intent(getApplicationContext(), conqueringQuestionMission.class);
//					Bundle mBundle = new Bundle();
//					for (int i=0; i<3; i++) {
//						mBundle.putInt(i+"", intent.getIntExtra(i+"", 0));
//					}
//					mBundle.putString("building", intent.getStringExtra("building"));
//					qIntent.putExtras(mBundle);
					startActivityForResult(qIntent, REQUEST_QUESTION_MISSION);
					finish();
				}
			}
		}
	}
	
	protected void onActivityResult(int requestCode, int resultCode, Intent data){
		if(resultCode == RESULT_OK){
			Toast.makeText(this, "you got point.", Toast.LENGTH_SHORT).show();
			setResult(RESULT_OK, intent);
			finish();
		}
	}

	private void doVibration() {
	    	
	    	// pattern: off, on, off, on,....
	    	long[] pattern = {0, 1000};
	    	
	    	// vibrate using the pattern. Second argument sets repetition from 
	    	// index 0. Use -1 if you don�셳 want repetition.
	    	vibrator.vibrate(pattern, -1); 

	    	//vibrator.vibrate(1000); // vibrate for 1 second (without pattern)

	    }
	  private void doVibration2() {
	    	
	    	// pattern: off, on, off, on,....
	    	long[] pattern = {0, 4000};
	    	
	    	// vibrate using the pattern. Second argument sets repetition from 
	    	// index 0. Use -1 if you don�셳 want repetition.
	    	vibrator.vibrate(pattern,-1); 

	    	//vibrator.vibrate(1000); // vibrate for 1 second (without pattern)

	    }
	public void onAccuracyChanged(Sensor arg0, int arg1) {
		// TODO Auto-generated method stub
	}

}
